package auto.app.controller;


import auto.app.config.DirectRabbitConfig;
import auto.app.controller.from.*;
import auto.app.controller.info.CasePageInfo;
import auto.app.controller.info.CollectionPageInfo;
import auto.app.exetion.HibernateExetion;
import auto.app.exetion.MainExection;
import auto.app.model.CaseDaoDto;
import auto.app.service.TestCollectionService;
import auto.app.util.CommonUtil;
import auto.app.util.Result;
import auto.app.util.UserGetAccount;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杨森
 * @since 2022-07-24
 */
@RestController
@RequestMapping("/testCollection")
@Api(value = "测试合集",tags = "测试合集")
public class TestCollectionController {

    @Autowired
    private TestCollectionService testCollectionService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping(value = "/add",name = "新增测试合集")
    @ApiOperation(value = "新增测试合集",notes = "新增测试合集")
    public Result<Boolean> addTestCollection(@Valid @RequestBody AddTestCollectionFrom addTestCollectionFrom) throws MainExection {
        Boolean aBoolean = testCollectionService.addTestCollection(addTestCollectionFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @PostMapping(value = "/detail/case",name = "查看测试用例详情")
    @ApiOperation(value = "查看测试用例详情",notes = "查看测试用例详情")
    public Result<CasePageInfo> caselistCollection(@Valid@RequestBody CollectionCasePageFrom collectionCasePageFrom) throws MainExection {
        CasePageInfo casePageInfo = testCollectionService.CaselistCollection(collectionCasePageFrom);
        return Result.getInstance(CasePageInfo.class).setData(casePageInfo);
    }

    @PostMapping(value = "/detail/deleteCase",name = "删除测试合集中的测试用例")
    @ApiOperation(value = "删除测试合集中的测试用例",notes = "删除测试合集中的测试用例")
    public Result<Boolean> deleteCase(@Valid @RequestBody CollectionCaseDeleteFrom collectionCaseDeleteFrom) throws MainExection {
        Boolean aBoolean = testCollectionService.deleteCase(collectionCaseDeleteFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @PostMapping(value = "/detail/add",name="新增测试合集中的测试用例")
    @ApiOperation(value = "新增测试合集中的测试用例",notes = "新增测试合集中的测试用例")
    public Result<Boolean> addTestCase(@Valid @RequestBody AddTestCollectionCaseFrom addTestCollectionCaseFrom) throws MainExection {
        Boolean aBoolean = testCollectionService.addTestCase(addTestCollectionCaseFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @PostMapping(value = "/sort",name = "测试用例排序")
    @ApiOperation(value = "测试用例排序",notes = "测试用例排序")
    public Result<Boolean> caseListSort(@Valid @RequestBody CollectionCaseSortFrom collectionCaseSortFrom) throws MainExection {
        Boolean aBoolean = testCollectionService.caseListSort(collectionCaseSortFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @PostMapping(value = "/list",name = "测试合集分页")
    @ApiOperation(value = "测试合集分页",notes = "测试合集分页")
    public Result<CollectionPageInfo> list( @RequestBody CollectionPage collectionPage){
        CollectionPageInfo list = testCollectionService.list(collectionPage);
        return Result.getInstance(CollectionPageInfo.class).setData(list);
    }
    @PostMapping(value = "/delete",name = "删除合集")
    @ApiOperation(value = "删除合集",notes = "删除合集")
    public Result<Boolean> deleteList(@Valid @RequestBody TestCollectionDeleteFrom testCollectionDeleteFrom) throws MainExection {
        Boolean aBoolean = testCollectionService.deleteList(testCollectionDeleteFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }
    @GetMapping(value = "/execute/{id}",name = "执行测试合集")
    @ApiOperation(value = "执行测试合集",notes = "执行测试合集")
    public Result<Boolean> execute(@PathVariable Integer id) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        map.put("bool",false);
        map.put("acconutId", UserGetAccount.getAccountId());
        map.put("accountName", UserGetAccount.getAccountName());
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend(DirectRabbitConfig.EXCHANGE_NAME, DirectRabbitConfig.ROUTINGKEY_NAME, map);
//        Boolean execute = testCollectionService.execute(id,false);
        return Result.getInstance(Boolean.class).setData(true);
    }
}

